import re
import json
import pymongo


class MongoConn:
    """
    提供MongoDB的集合对象
    """

    def __init__(self):
        # mongodb ver 4.4.2
        self._client = pymongo.MongoClient(f"mongodb://{user}:{pwd}@localhost:27017/?authSource=voa")
        self._db = self._client[db_name]
        self._col = self._db[collection_name]

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._client.close()

    # 数据库创建及插入信息
    def com_log(self, init_details, from_tmp=True):
        if from_tmp:
            # 断点续传模式：读取库中所有需要下载的记录，返回相对链接和目录
            done = [kv['_id'] for kv in self._col.find({}, {"_id": 1})]
            new_details = [fd for fd in init_details if fd[0].split("/")[-1] not in done]
            return new_details
        else:
            # 全新下载模式
            return init_details

    def update(self, dict_):
        insert_ = self._col.insert_one(dict_)
        return insert_.inserted_id

    def search(self, field, key):
        query = {field: {"$regex": key}}
        doc = self._col.find(query)
        for data in doc:
            print(json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))


# 下载目录
download_dir = 'F:/Download/'

user = 'voadmin'
pwd = '911911911'
db_name, collection_name = "voa", "voa"  # 缓存数据库的数据库
failed = "failed.log.txt"  # 失败日志输出文件

from_tmp = True  # 是否从缓存下载，初次下载请False，断点续传请True
num = 5  # 实际下载数量
timeout = 5  # 超时设置

domain = 'https://www.51voa.com/'  # 网站域名
init_link = "VOA_Standard_1.html"  # 初始页面相对路径

cata_selector = "#Right_Content > div.pagelist > a:nth-child(1n+1)"  # 目录页链接选择器
date_selector = "#Right_Content > div.List > ul > li:nth-child(1n+1)"  # 发布日期选择器，发布日期将作为文件夹命名
detail_link_selector = "#Right_Content > div.List > ul > li:nth-child(1n+1) > a"  # 详情页链接选择器
voice_link_selector = '#mp3'
selectors = {'title': '#Right_Content > div.title > h1',
             'author': '#Right_Content > div.Content > span.byline',
             'date': '#Right_Content > div.Content > span.datetime',
             'text': '#Right_Content > div.Content > p:nth-child(1n+1)'}
related_articles_selector = '#Right_Content > div.List > ol > li:nth-child(1n+1) > a'

# 日期提取正则模式
date_pattern = re.compile("1[4-9]-[1]?[0-9]")

# http请求头
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"}
